/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager;

import br.com.foxline.clinicplus.entity.Income;
import br.com.foxline.clinicplus.enums.TypeCredit;
import br.com.foxline.clinicplus.enums.TypeDebt;
import br.com.foxline.clinicplus.enums.TypeIncome;
import br.com.foxline.clinicplus.service.DoctorService;
import br.com.foxline.clinicplus.service.IncomeService;
import br.com.foxline.clinicplus.service.ProcedureService;
import br.com.foxline.clinicplus.service.SystemUserService;
import br.com.foxline.clinicplus.util.Msg;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerFinancialReceitas implements Serializable {

    @EJB
    IncomeService incomeService;
    @EJB
    DoctorService doctorService;
    @EJB
    ProcedureService procedureService;
    @EJB
    SystemUserService systemUserService;
    private Income income;
    private List<Income> incomes;
    private BigDecimal total;

    @PostConstruct
    public void init() {
        income = new Income();
        income.setTypeIncome(TypeIncome.R);
        incomes = new ArrayList<Income>();
        total = new BigDecimal(0);
    }

    public List<SelectItem> selectTypeCredit() {
        List<SelectItem> list = new ArrayList<SelectItem>();

        for (TypeCredit ms : TypeCredit.values()) {
            list.add(new SelectItem(ms, ms.getDesc()));
        }
        return list;
    }

    public Income getIncome() {
        return income;
    }

    public void setIncome(Income income) {
        this.income = income;
    }

    public List<Income> getIncomes() {
        return incomes;
    }

    public void setIncomes(List<Income> incomes) {
        this.incomes = incomes;
    }

    public void add() {
        income.setAmountPaid(income.getTotalAmount());
        total = total.add(income.getTotalAmount());

        if (income.getCreateAt() != null) {
            Calendar gc1 = GregorianCalendar.getInstance();
            income.setCreateAt(gc1.getTime());
        }
        income.setSystemUser(systemUserService.getCurrentUser());
        incomes.add(income);
        income = new Income();
        income.setTypeIncome(TypeIncome.R);
        income.setFaturamentoImediato(true);

    }

    public void salvar() {
        if(incomes == null || incomes.isEmpty()){
            Msg.messagemError("Adicione um elemento");
            return;
        }
        try {
            for (Income i : incomes) {
                incomeService.save(i);
            }
            incomes = new ArrayList<Income>();
            total = BigDecimal.ZERO;
            Msg.messagemInfo("Cadastrado com Sucesso !");
        } catch (Exception e) {
            Msg.messagemError("Erro !");
        }

    }

    public BigDecimal getTotal() {
        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }
    
}
